package com.gt.xmall.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.gt.xmall.model.GtdContext;
import com.gt.xmall.service.GtdContextService;
import com.gt.xmall.web.BaseController;

import com.gt.xmall.model.GtdProject;
import com.gt.xmall.service.GtdProjectService;
import com.gt.xmall.util.ServletUtil;

@Controller
@RequestMapping(value = "/gtd/gtdproject/")
public class GtdProjectController extends BaseController {
	
	@Autowired
	private GtdProjectService gtdProjectService;
	
	@RequestMapping(value = "list",method = RequestMethod.GET)
	public String list(Model model){
		List<GtdProject> list = gtdProjectService.getGtdProjects();
		model.addAttribute("gtdProjectList", list);
		return "gtd/gtdProjectList";
	}
	@RequestMapping(value = "show/{id}",method = RequestMethod.GET)
	public String show(@PathVariable("id") String id,Model model){
		GtdProject gtdProject =gtdProjectService.getGtdProject(new Long(id));
		model.addAttribute("gtdProject", gtdProject);
		return "gtd/gtdProjectForm";
		
	}
	@RequestMapping(value="save",method = RequestMethod.POST)
	public String save(@Valid GtdProject gtdProject,RedirectAttributes redirectAttributes){
		gtdProject = gtdProjectService.saveGtdProject(gtdProject);
//		redirectAttributes.addAttribute("gtdContext", gtdContext);
		return "redirect:/gtd/gtdproject/show/"+gtdProject.getId();
	} 
	@RequestMapping(value = "new",method = RequestMethod.GET)
	public String blankForm(){
		return "gtd/gtdProjectForm";
	}
	
	@RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
	public String delete(@PathVariable("id") String id,Model model){
		gtdProjectService.deleteGtdProject(new Long(id));
		
		return "redirect:/gtd/gtdproject/list";
		
	}
	
	@RequestMapping(value = "listpage",method = RequestMethod.GET)
	public String listPage(Model model,HttpServletRequest request,HttpServletResponse response){
//		List<GtdProject> list = gtdProjectService.getGtdProjects();
	 
		Page<GtdProject> page = gtdProjectService.getProjectPage( gtdProjectService.initPageRequest("gtdProjectTable", request, response, 1), ServletUtil.initSearchFilters(request));
		model.addAttribute("gtdProjectList", page.getContent());
		model.addAttribute("gtdProjectTable_totalRows", gtdProjectService.getTotalRow(request));
		if("export".equals(request.getParameter("exportFlag"))){
			byte[] contents = view.getBytes();
            response.getOutputStream().write(contents);
			return null;
		}
		return "gtd/gtdProjectPage";
	}
	
}